package univ.bme.accounting.beans;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor

public class MBUtils{
		@ManagedProperty("#{msgs}")
		protected ResourceBundle bundle;
		
		protected HttpServletRequest getRequest() {
			return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		}
	
	//These can be shown with the <h:messages /> tag
		protected void sendInfoMessageToUser(String message){
			FacesContext context = getContext();
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
		}
		
		protected void sendErrorMessageToUser(String message){
			FacesContext context = getContext();
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
		}
		
		protected FacesContext getContext() {
			FacesContext context = FacesContext.getCurrentInstance();
			return context;
		}
		
		protected String getSha256(String text) throws NoSuchAlgorithmException {  
			  MessageDigest md = MessageDigest.getInstance("SHA-256"); 
			  md.update(text.getBytes());
			  return DatatypeConverter.printBase64Binary(md.digest());
		 }
}
